package com.thinkingcoder.tcrpc.core.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thinkingcoder.tcrpc.core.balancer.LoadBalancer;
import com.thinkingcoder.tcrpc.core.config.ConsumerProperties;
import com.thinkingcoder.tcrpc.core.filter.Filter;
import com.thinkingcoder.tcrpc.core.meta.InstanceMeta;
import com.thinkingcoder.tcrpc.core.router.Router;

import lombok.Data;

@Data
public class RpcContext {
    List<Filter> filters;

    Router<InstanceMeta> router;

    LoadBalancer<InstanceMeta> loadBalancer;

    private Map<String, String> parameters = new HashMap<>();

    private ConsumerProperties consumerProperties;

    public String param(String key) {
        return parameters.get(key);
    }

    // tcrpc.color = gray
    // tcrpc.gtrace_id
    // gw -> service1 -> service2(跨线程传递) ...
    // http headers
    public static InheritableThreadLocal<Map<String, String>> ContextParameters = new InheritableThreadLocal<>() {
        @Override
        protected Map<String, String> initialValue() {
            return new HashMap<>();
        }
    };

    public static void setContextParameter(String key, String value) {
        ContextParameters.get().put(key, value);
    }

    public static String getContextParameter(String key) {
        return ContextParameters.get().get(key);
    }

    public static void removeContextParameter(String key) {
        ContextParameters.get().remove(key);
    }
}
